//
//  NovelSiftViewController.m
//  novel
//
//  Created by 高青松 on 2018/8/11.
//  Copyright © 2018年 Tracy. All rights reserved.
//

#import "NovelSiftViewController.h"
#import "SDCycleScrollView.h"
#import "NovelSiftFirstTableViewCell.h"
#import "NovelThreeTableViewCell.h"
#import "NovelSiftSecondTableViewCell.h"
#import "NovelViewModel.h"
#import "novelHeaderModel.h"
#import "novelFirstModel.h"
#import <YYReachability.h>
@interface NovelSiftViewController ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>
@property (nonatomic,strong)UITableView * tableView;
@property (nonatomic,strong)SDCycleScrollView * cycleScrollView;
@property (nonatomic,strong)NSMutableArray * jsonArray;
@end

static NSString * FirstCellID = @"FirstCellID";
static NSString * SecondCellID = @"SecondCellID";
static NSString * ThreeCellID = @"ThreeCellID";
static NSString * secondCellID = @"secondCellID";

@implementation NovelSiftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.jsonArray = [[[NovelViewModel alloc]init] loadData];
    
    
    if([YYReachability reachability].status == YYReachabilityStatusNone){
        self.view.backgroundColor = [UIColor whiteColor];
        self.view.alpha = 0;
        return;
    };
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.top.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-49);
//        [self.tableView setBackgroundColor:COLORABABAB];

    }];
    adjustsScrollViewInsets_NO(self.tableView, self);
    
    
}

//刷新
- (void)getRefreshData
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_header endRefreshing];
        self.jsonArray = [[[NovelViewModel alloc]init] loadData];
        [self.tableView reloadData];
    });
}

//获取更多数据
- (void)getMoreData
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_footer endRefreshing];
        [self.view makeToast:@"没有更多数据哦！请稍后在尝试" duration:1.0f position:CSToastPositionCenter];
    });
    
}

#pragma mark --delegate--
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2) {
        NSArray * novelListArray = self.jsonArray[3];
        CGFloat threeCellH = 0;
        for (NSDictionary * dataDic in novelListArray) {
            NSArray * datasouceArray = [dataDic objectForKey:@"dataSource"];
            threeCellH = threeCellH + (datasouceArray.count/3 + (datasouceArray.count % 3 > 0 ? 1 : 0 )) * 160 * kProportionW;
        }
        return threeCellH + 50 * kProportionW * novelListArray.count ;
    }else if (indexPath.section == 0){
       return 360 * kProportionW;
    }else{
        return 280 * kProportionW;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10 * kProportionW;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        NovelSiftFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstCellID];
        novelFirstModel * firstModel1 = self.jsonArray[1][0];
        novelFirstModel * firstModel2 = self.jsonArray[1][1];
        novelFirstModel * firstModel3 = self.jsonArray[1][2];
        novelFirstModel * firstModel4 = self.jsonArray[1][3];
        [cell.userWelfareButton sd_setImageWithURL:[NSURL URLWithString:firstModel1.pic] forState:UIControlStateNormal];
        [cell.userWelfareButton addTarget:self action:@selector(userWelfareAction) forControlEvents:UIControlEventTouchUpInside];
        [cell.goodBookButton sd_setImageWithURL:[NSURL URLWithString:firstModel2.pic] forState:UIControlStateNormal];
        [cell.goodBookButton addTarget:self action:@selector(goodBookAction) forControlEvents:UIControlEventTouchUpInside];
        [cell.girlHotButton sd_setImageWithURL:[NSURL URLWithString:firstModel3.pic] forState:UIControlStateNormal];
        [cell.girlHotButton addTarget:self action:@selector(girlHotAction) forControlEvents:UIControlEventTouchUpInside];
        [cell.boyHotButton sd_setImageWithURL:[NSURL URLWithString:firstModel4.pic] forState:UIControlStateNormal];
        [cell.boyHotButton addTarget:self action:@selector(boyHotAction) forControlEvents:UIControlEventTouchUpInside];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.section == 1){
        NovelSiftSecondTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:secondCellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dataArray = self.jsonArray[2];
        return cell;
        
    }else{
        NovelThreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ThreeCellID];
        cell.dataArray = self.jsonArray[3];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    return nil;
}

#pragma mark --method--
- (void)userWelfareAction
{
    NSLog(@"----新手礼包");
}

- (void)goodBookAction
{
    NSLog(@"----好书畅读");
}

- (void)girlHotAction
{
    NSLog(@"----女生完结");
}

- (void)boyHotAction
{
    NSLog(@"----男生畅销");
}

#pragma mark --banner跳转--
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"--轮播图---%ld",index);
}

#pragma mark  --set and get--
- (UITableView *)tableView
{
    if (!_tableView) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        self.tableView.tableHeaderView = self.cycleScrollView;
        self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.separatorStyle = UITableViewCellAccessoryNone;
        self.tableView.showsVerticalScrollIndicator = NO;
        self.tableView.showsHorizontalScrollIndicator = NO;
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.tableView registerClass:[NovelSiftFirstTableViewCell class] forCellReuseIdentifier:FirstCellID];
        [self.tableView registerClass:[NovelThreeTableViewCell class] forCellReuseIdentifier:ThreeCellID];
        [self.tableView registerClass:[NovelSiftSecondTableViewCell class] forCellReuseIdentifier:secondCellID];
        

//        //下拉刷新
        __weak typeof(self) weakSelf = self;
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            // 进入刷新状态后会自动调用这个block
            [weakSelf getRefreshData];
        }];
        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakSelf getMoreData];
        }];
    }
    return _tableView;
}

- (SDCycleScrollView *)cycleScrollView
{
    if (!_cycleScrollView) {
        self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0,0, kScreenW, 150 * kProportionW) delegate:self placeholderImage:[UIImage imageNamed:@"Host_BannerImg"]];
        self.cycleScrollView.backgroundColor = [UIColor whiteColor];
        self.cycleScrollView.delegate = self;
        self.cycleScrollView.autoScrollTimeInterval = 4;
        self.cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        self.cycleScrollView.currentPageDotColor = [UIColor whiteColor];
        NSMutableArray *imageURLArray = [NSMutableArray array];
        for (novelHeaderModel * headerModel in self.jsonArray[0]) {
            [imageURLArray addObject:headerModel.pic];
        }
        self.cycleScrollView.imageURLStringsGroup = imageURLArray;
    }
    return _cycleScrollView;
}
@end;
